using System;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;
using BaseClass;

namespace WindowsApplication1
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
            Form3.CheckForIllegalCrossThreadCalls = false;
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            SendText sendText = new SendText("localhost", 8000, "111", null);
            sendText.Send();
        }

        private void StartSendText(string strHost, int iPort, string strInfo)
        {
            SendText stText = new SendText(strHost, iPort, strInfo, new CommunClass.OnSend(OnSendDrawProgress));
            StartThread(new ThreadStart(stText.Send));
        }

        private void StartThread(ThreadStart target)
        {
            Thread doStep = new Thread(target);
            doStep.IsBackground = true;
            doStep.Start();
        }

        public void OnSendDrawProgress(int iTotal, int iSending)
        {
            if (iTotal != pbMain.Maximum)
                pbMain.Maximum = iTotal;
            pbMain.Value = iSending;
        }
    }

    public class SendText
    {
        private string Host;
        private int Port;
        private string Info;
        private CommunClass.OnSend onsend;

        public SendText(string strHost, int iPort, string strInfo,
             CommunClass.OnSend onSend)
        {
            Host = strHost;
            Port = iPort;
            Info = strInfo;
            onsend = onSend;
        }

        public void Send()
        {
            Socket s = null;
            try
            {
                s = CommunClass.ConnectToServer(Host, Port);
                CommunClass.WriteCommandToSocket(s, "SENDTEXT");
                CommunClass.WriteCommandDescToSocket(s, "");
                CommunClass.WriteDynamicTextToSocket(s, Info, onsend);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            finally
            {
                if (s != null)
                    s.Close();
            }
        }
    }//end class
}